package com.qianfeng.openapi.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.bean.AjaxMessage;
import com.qianfeng.openapi.bean.TableData;
import com.qianfeng.openapi.pojo.ApiMapping;
import com.qianfeng.openapi.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys/api_mapping")
public class ApiMappingController {
    @Autowired
    private ApiMappingService apiMappingService;

    @RequestMapping("/add")
    public AjaxMessage insertApiMapping(ApiMapping apiMapping){
        apiMappingService.insertApiMapping(apiMapping);
        return new AjaxMessage(true, "添加成功");
    }

    @RequestMapping("/del")
    public AjaxMessage deleteApiMapping(int[] ids){
        apiMappingService.deleteApiMapping(ids);
        return new AjaxMessage(true, "删除成功");
    }

    @RequestMapping("/update")
    public AjaxMessage updateApiMapping(ApiMapping apiMapping){
        apiMappingService.updateApiMapping(apiMapping);
        return new AjaxMessage(true, "修改成功");
    }

    @RequestMapping("/table")
    public TableData<ApiMapping> getAllApiMapping(int page, int limit, ApiMapping apiMapping, String gatewayApiName, Integer state){
        ApiMapping apiMapping1 = new ApiMapping();
        apiMapping1.setGatewayApiName(gatewayApiName);
        apiMapping1.setState(state);
        PageInfo<ApiMapping> pageInfo = apiMappingService.getAllApiMapping(page, limit, apiMapping);
        TableData<ApiMapping> tableData=new TableData<>();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(pageInfo.getList());
        return tableData;
    }
}
